package com.MusicWeb.servlet;


import com.MusicWeb.entity.User;
import com.MusicWeb.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String acc = req.getParameter("account");
        String pwd = req.getParameter("password");
        String name=req.getParameter("name");

        User user = new User();
        user.setName(name);
        user.setAccount(pwd);
        user.setPassword(acc);

        if (userService.register(user) != null) {
            req.getSession().setAttribute("msg", "注册成功");
            resp.sendRedirect(req.getContextPath() + "/login.jsp");
        } else {
            req.getSession().setAttribute("msg", "注册失败");
            resp.sendRedirect(req.getContextPath() + "/register.jsp");
        }
    }
}